package cn.powerone.autotest.service.impl;

import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.powerone.common.annotation.DataSource;
import cn.powerone.common.enums.DataSourceType;
import cn.powerone.common.exception.ServiceException;
import cn.powerone.common.utils.StringUtils;
import cn.powerone.autotest.domain.ProjectMember;
import cn.powerone.autotest.mapper.ProjectMemberMapper;
import cn.powerone.autotest.service.IProjectMemberService;

/**
 * 项目成员管理 服务层实现
 * 
 * @author autotest
 */
@Service
@DataSource(DataSourceType.MASTER)
public class ProjectMemberServiceImpl implements IProjectMemberService
{
    @Autowired
    private ProjectMemberMapper projectMemberMapper;

    /**
     * 查询项目成员列表
     * 
     * @param projectId 项目ID
     * @return 项目成员集合
     */
    @Override
    public List<ProjectMember> selectProjectMembersByProjectId(String projectId)
    {
        return projectMemberMapper.selectProjectMembersByProjectId(projectId);
    }

    /**
     * 添加项目成员
     * 
     * @param projectMember 项目成员信息
     * @return 结果
     */
    @Override
    public int insertProjectMember(ProjectMember projectMember)
    {
        if (StringUtils.isAnyEmpty(projectMember.getProjectId(), projectMember.getUserId(), projectMember.getRole()))
        {
            throw new ServiceException("项目成员信息不完整");
        }

        // 检查成员是否已存在
        if (checkProjectMemberExists(projectMember.getProjectId(), projectMember.getUserId()))
        {
            throw new ServiceException("该用户已经是项目成员");
        }

        // 设置唯一ID
        projectMember.setId(UUID.randomUUID().toString());
        return projectMemberMapper.insertProjectMember(projectMember);
    }

    /**
     * 更新项目成员角色
     * 
     * @param projectMember 项目成员信息
     * @return 结果
     */
    @Override
    public int updateProjectMemberRole(ProjectMember projectMember)
    {
        if (StringUtils.isAnyEmpty(projectMember.getProjectId(), projectMember.getUserId(), projectMember.getRole()))
        {
            throw new ServiceException("项目成员信息不完整");
        }

        // 检查成员是否存在
        if (!checkProjectMemberExists(projectMember.getProjectId(), projectMember.getUserId()))
        {
            throw new ServiceException("项目成员不存在");
        }

        return projectMemberMapper.updateProjectMemberRole(projectMember);
    }

    /**
     * 删除项目成员
     * 
     * @param projectId 项目ID
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public int deleteProjectMember(String projectId, String userId)
    {
        if (StringUtils.isAnyEmpty(projectId, userId))
        {
            throw new ServiceException("项目ID和用户ID不能为空");
        }

        // 检查成员是否存在
        if (!checkProjectMemberExists(projectId, userId))
        {
            throw new ServiceException("项目成员不存在");
        }

        ProjectMember projectMember = new ProjectMember();
        projectMember.setProjectId(projectId);
        projectMember.setUserId(userId);
        return projectMemberMapper.deleteProjectMember(projectMember);
    }

    /**
     * 校验项目成员是否存在
     * 
     * @param projectId 项目ID
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public boolean checkProjectMemberExists(String projectId, String userId)
    {
        ProjectMember projectMember = new ProjectMember();
        projectMember.setProjectId(projectId);
        projectMember.setUserId(userId);
        return projectMemberMapper.checkProjectMemberExists(projectMember) > 0;
    }
}